package com.sz.sidi.dao.jdbc.impl;

import java.util.List;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import com.sz.sidi.dao.jdbc.BaseJdbcDao;
import com.sz.sidi.entity.TuserInfo;
import com.sz.sidi.exceptions.DaoException;
import com.sz.sidi.util.ObjectMapper;

public class BaseJdbcDaoImpl extends JdbcDaoSupport implements BaseJdbcDao{
	protected Logger logger = Logger.getLogger(getClass());
	
	public TuserInfo getUserInfo(String fgh) throws DaoException {
		TuserInfo userinfo = null;
		try {
			String sql = "SELECT * FROM TUSERINFO where fgh = ?";
			List<TuserInfo> list = this.getJdbcTemplate().query(sql,
					new Object[] { fgh }, new ObjectMapper(TuserInfo.class));
			if (list != null && list.size() > 0) {
				userinfo = list.get(0);
			}
			//logger.info("getLoginUser=" + sql);
			logger.log(Level.INFO, "getLoginUser=" + sql);
		} catch (Exception e) {
			throw new DaoException(e.getMessage());
		}
		return userinfo;
	}

	public SqlRowSet getEnity(String table,String wheresql, String ordrebysql)
			throws DaoException {
		SqlRowSet rs = null;
		try {
			StringBuffer buffer = new StringBuffer();
			if (table != null && !table.equals("")) {
				buffer.append("select * from " + table);
			}

			if (wheresql != null && !wheresql.equals("")) {
				buffer.append(" " + wheresql);
			}

			if (ordrebysql != null && !ordrebysql.equals("")) {
				buffer.append(" " + ordrebysql);
			}
			rs = this.getJdbcTemplate().queryForRowSet(buffer.toString());
			logger.info("getEnity=" + buffer.toString());
		} catch (Exception e) {
			throw new DaoException(e.getMessage());
		}
		return rs;
	}

}
